********************************************************************************
*Replication File: Individual (ESS) Analysis
********************************************************************************

********************************************************************************
*For use with ESS.dta file
*Data source - ESS Cummulative file Pulled 18 May 2023


*******************************************************************************
*Data Analysis for Figure 5 and Appendix Table B.3.1.
*******************************************************************************


*Models used regarding the DV: Feel emotionally close to country*
*Baseline for Immigrants
eststo A1: qui mixed attachc municipal i.year i.rcode ///
	if (noncit == 1 | immigrant == 1) & SEU28_nat!=1  ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
*Covariate Model for Immigrants
eststo A: qui mixed attachc municipal i.rcode ///
	yearslived imm_par cntrylang noncit ///
	female employed  married agea rural religious  i.year ///
	if (noncit == 1 | immigrant == 1) & SEU28_nat!=1 ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
*Covariate Model for Non-Citizens
eststo ImmO: qui mixed attachc municipal i.rcode ///
	yearslived imm_par cntrylang  ///
	female employed  married agea rural religious  i.year ///
	if (noncit == 1) & SEU28_nat!=1 ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
*Covariate Model for Eligible, Non-Citizens
eststo Imm: qui mixed attachc eligible i.rcode ///
	yearslived imm_par cntrylang ///
	female employed  married agea rural religious  i.year ///
	if (noncit == 1) & SEU28_nat!=1 ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
*Covariate Model for Non-Immigrant Citizens
eststo AR: qui mixed attachc municipalC i.rcode ///
	imm_par female employed  married agea rural religious i.year ///
	if noncit == 0 & immigrant == 0 ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
	
*Models used regarding the DV: Feel emotionally close to Europe*
*Baseline for Immigrants
eststo C1: qui mixed attache municipal i.year i.rcode ///
	if (noncit == 1 | immigrant == 1) & SEU28_nat!=1 ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
*Covariate Model for Immigrants
eststo C: qui mixed attache municipal i.rcode ///
	yearslived imm_par cntrylang noncit ///
	female employed  married agea rural religious  i.year ///
	if (noncit == 1 | immigrant == 1) & SEU28_nat!=1  ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
*Covariate Model for Non-Citizens
eststo ImmOE: qui mixed attache municipal i.rcode ///
	yearslived imm_par cntrylang  ///
	female employed  married agea rural religious  i.year ///
	if (noncit == 1) & SEU28_nat!=1  ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
*Covariate Model for Eligible, Non-Citizens
eststo ImmE: qui mixed attache eligible i.rcode ///
	yearslived imm_par cntrylang ///
	female employed  married agea rural religious  i.year ///
	if (noncit == 1) & SEU28_nat!=1 ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
*Covariate Model for Non-Immigrant Citizens
eststo CR: qui mixed attache municipalC i.rcode ///
	imm_par female employed  married agea rural religious i.year ///
	if noncit == 0 & immigrant == 0  ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
	
*Models used regarding the DV: Feel close to local people*

****NOTE FOR JOP REPLICATION TEAM:
	***A typographical error in models local1 and local2 erroneously dropped
	***cases (1251 and 662, respectively). Specifically, these two models applied an 
	***inappropriately stringent exclusion criteria that does not match the
	***prior models. This line of code for local1 and local2 was: & SEU28_imm!=1
	***This has been fixed (i.e., omitted) below and the accurate results are 
	***reflected in the updated manuscript. Specifically, this affected 
	***Figure 5 in the main text and Appendix Table 2 in B.3.2.
	***The estimates are still within the confidence intervals, remain
	***insignificant, and are/were not substantively interpretted.
	
*Baseline for Immigrants	
eststo local1: qui mixed peopleclose municipal i.year i.rcode ///
	if (noncit == 1 | immigrant == 1) & SEU28_nat!=1  ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
*Covariate Model for Immigrants
eststo local2: qui mixed peopleclose municipal i.rcode ///
	yearslived imm_par cntrylang noncit ///
	female employed  married agea rural religious  i.year  ///
	if (noncit == 1 | immigrant == 1) & SEU28_nat!=1  ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
*Covariate Model for Non-Citizens
eststo ImmOlocal: qui mixed peopleclose municipal i.rcode ///
	yearslived imm_par cntrylang  ///
	female employed  married agea rural religious  i.year  ///
	if (noncit == 1) & SEU28_nat!=1  ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
*Covariate Model for Eligible, Non-Citizens
eststo Immlocal: qui mixed peopleclose eligible i.rcode ///
	yearslived imm_par cntrylang ///
	female employed  married agea rural religious  i.year  ///
	if (noncit == 1) & SEU28_nat!=1 ///
	[weight=pspwght] || rcode:, robust cluster(rcode)
*Covariate Model for Non-Immigrant Citizens
eststo localR: qui mixed peopleclose municipalC i.rcode ///
	imm_par female employed  married agea rural religious i.year ///
	if noncit == 0 & immigrant == 0  ///
	[weight=pspwght] || rcode:, robust cluster(rcode)

*******************************************************************************
*Figure Replication
*******************************************************************************
	
*Figure 5
graph set window fontfacemono "Helvetica" 
	
coefplot (A1, label(Non-EU Immigrants, w/o covariates) msize(small) mcolor("165 15 21") ///
	ciopts(lcolor("165 15 21" "165 15 21") lwidth(.3 .7)) keep(municipal)) ///
	(A, label(Non-EU Immigrants, w/covariates) msize(small) mcolor("222 45 38") ///
		msymbol(o) ciopts(lcolor("222 45 38" "222 45 38") lwidth(.2 .4)) keep(municipal)) ///
	(ImmO, label(Non-EU Immigrants, w/covariates) msize(small) mcolor("251 106 74") ///
		msymbol(O) ciopts(lcolor("251 106 74" "251 106 74") lwidth(.2 .4)) keep(municipal)) ///
	///(Imm, label(Non-EU Immigrants, w/covariates) msize(small) mcolor("252 174 145") ///
	///	msymbol(O) ciopts(lcolor("252 174 145" "252 174 145") lwidth(.2 .4)) keep(eligible)) /// 
	(AR, label(Non-Immigrant Citizens, w/covariates) msize(small) mcolor("97 156 255") ///
		msymbol(d) ciopts(lcolor("97 156 255" "97 156 255") lwidth(.2 .4)) keep(municipalC)), ///
		bylabel(Country Attachment) plotregion(lcolor(gs5)) || ///
	(C1, label(Non-EU Immigrants, w/o covariates) msize(small) mcolor("165 15 21") ///
		msymbol(O)  ciopts(lcolor("165 15 21" "165 15 21") lwidth(.2 .4)) keep(municipal)) ///
	(C, label(Non-EU Immigrants, w/covariates) msize(small) mcolor("222 45 38") ///
		ciopts(lcolor("222 45 38" "222 45 38") lwidth(.3 .7)) keep(municipal)) ///
	(ImmOE, label(Non-EU Immigrants, w/covariates) msize(small) mcolor("251 106 74") ///
		msymbol(O) ciopts(lcolor("251 106 74" "251 106 74") lwidth(.2 .4)) keep(municipal)) ///
	///(ImmE1, label(Non-EU Immigrants, w/covariates) msize(small) mcolor("252 174 145") ///
		///msymbol(O) ciopts(lcolor("252 174 145" "252 174 145") lwidth(.2 .4)) keep(eligible)) /// 
	(CR, label(Non-Immigrant Citizens, w/covariates) msize(small) mcolor("97 156 255") ///
		msymbol(d) ciopts(lcolor("97 156 255" "97 156 255") lwidth(.2 .4)) keep(municipalC)), ///
		bylabel(Europe Attachment) plotregion(lcolor(gs5)) || ///
	(local1, label(Non-EU Immigrants, w/o covariates) msize(small) mcolor("165 15 21") ///
		ciopts(lcolor("165 15 21" "165 15 21") lwidth(.2 .4)) keep(municipal)) ///
	(local2, label(Non-EU Immigrants, w/covariates) msize(small) mcolor("222 45 38") ///
		msymbol(O) ciopts(lcolor("222 45 38" "222 45 38") lwidth(.2 .4)) keep(municipal)) ///
	(ImmOlocal, label(Non-EU Immigrants, w/covariates) msize(small) mcolor("251 106 74") ///
		msymbol(O) ciopts(lcolor("251 106 74" "251 106 74") lwidth(.2 .4)) keep(municipal)) ///
	///(Immlocal, label(Non-EU Immigrants, w/covariates) msize(small) mcolor("252 174 145") ///
		///msymbol(O) ciopts(lcolor("252 174 145" "252 174 145") lwidth(.2 .4)) keep(eligible)) /// 
	(localR, label(Non-Immigrant Citizens, w/covariates) msize(small) mcolor("8 81 156") ///
		msymbol(d) ciopts(lcolor("8 81 156" "8 81 156") lwidth(.2 .4)) keep(municipalC)), ///
			bylabel(Close to Locality) ||, ///
	bycoefs byopts(yrescale) vertical rename(municipalC = municipal eligible = municipal) ///
	legend(order(3 "Non-EU Immigrants," "w/o covariates" ///
			6 "Non-EU Immigrants," "w/covariates" ///
			9 "Non-EU Citizens," "w/covariates" ///
			12 "Non-Immigrant Citizens," "w/covariates") ///
			title({bf:Model}, size(small) pos(12) color(gs5)) ///
			region(lcolor(gs5)) size(vsmall) color(gs5) col(1) pos(3)) ///
		levels(95 90) ///
		yline(0, lcolor(gs5) lpattern(dash)) ///
		xlabel(, labsize(small) labcolor(gs5)) ///
		ylabel(, glcolor(gs15) ang(0) labsize(small) labcolor(gs5)) ///
		xline(1.5 2.5 3.5, lcolor(gs9)) ///
	graphregion(color(white) lcolor(gs5)) ///
		plotregion(lcolor(gs5)) yline(0, lcolor(gs5) lpattern(dash)) ///
		title("Predicting Attachment by Non-EU Suffrage" " ", ///
			bexpand color(gs5) bcolor(white) pos(11) size(medsmall)) ///
		grid(lcolor(gs5)) ///
		yscale(lcolor(gs5) noex) ///
		xscale(lcolor(gs5) noex) bgcol(white)
	
graph export Figure5.png, replace

*******************************************************************************
*Table Replication
*******************************************************************************

*Appendix Table B.3.1 (p.24)
esttab A1 A ImmO using AppendixB31.tex, se(3) b(3) aic ///
	nogaps star(+ 0.1 * 0.05 ** 0.01) ///
	drop(*rcode) ///
	indicate(Fixed Effects = *year, labels("Country \& Year" "--")) ///
	label nonumbers ///
	addnotes("Unstandardized beta coefficients with robust clustered standard errors in parentheses." ///
	"Country random intercept. Weighted by PSPWeight. +\textit{p} $<$ 0.10 * \textit{p} $<$ 0.05, ** \textit{p} $<$ 0.01.") nonotes replace

*Appendix Table B.3.1 (p.25)
esttab C1 C ImmOE local1 local2 ImmOlocal using AppendixB312.tex, se(3) b(3) aic ///
	nogaps star(+ 0.1 * 0.05 ** 0.01) ///
	drop(*rcode) ///
	indicate(Fixed Effects = *year, labels("Country \& Year" "--")) ///
	label nonumbers ///
	addnotes("Unstandardized beta coefficients with robust clustered standard errors in parentheses." ///
	"Country random intercept. Weighted by PSPWeight. +\textit{p} $<$ 0.10 * \textit{p} $<$ 0.05, ** \textit{p} $<$ 0.01.") nonotes replace

*Appendix Table C.3.1
esttab Imm AR ImmE CR Immlocal localR using AppendixC31.tex, se(3) b(3) aic ///
	nogaps star(+ 0.1 * 0.05 ** 0.01) ///
	order(eligible municipal yearslived) ///
	drop(*rcode) ///
	indicate(Fixed Effects = *year, labels("Country \& Year" "--")) ///
	label nonumbers ///
	addnotes("Unstandardized beta coefficients with robust clustered standard errors in parentheses." ///
	"Country random intercept. Weighted by PSPWeight. +\textit{p} $<$ 0.10 * \textit{p} $<$ 0.05, ** \textit{p} $<$ 0.01.") nonotes replace

********************************************************************************
*Descriptive Table 
********************************************************************************
*Data for Appendix A.3.2
//Immigrants
sum municipal female employed married agea religious ///
	imm_par cntrylang noncit rural yearslived if ///
	(noncit == 1 | immigrant == 1) & SEU28_nat!=1 & ///
	(attachc!=. | attache!=. | peopleclose!=.) & ///
	(municipal!=. & female!=. & employed!=. & married!=. & agea!=. & religious!=. & ///
	imm_par!=. & cntrylang!=. & noncit!=. & rural!=. & yearslived!=.) [weight=pspwght]
//Citizens
sum municipalC female employed married agea religious ///
	imm_par cntrylang noncit rural if ///
	noncit == 0 & immigrant == 0 &  ///
	(attachc!=. | attache!=. | peopleclose!=.) & ///
	(municipalC!=. & female!=. & employed!=. & married!=. & agea!=. & religious!=. & ///
	imm_par!=. & rural!=.) [weight=pspwght]
